export function formatRelativeTime(timestamp) {
  if (!timestamp) return ''
  
  const now = new Date()
  const date = new Date(timestamp)
  const diff = now - date
  const diffMinutes = Math.floor(diff / (1000 * 60))
  const diffHours = Math.floor(diff / (1000 * 60 * 60))
  const diffDays = Math.floor(diff / (1000 * 60 * 60 * 24))
  
  // 判断是否是今年
  const isThisYear = now.getFullYear() === date.getFullYear()
  
  // 1分钟内
  if (diffMinutes < 1) {
    return '刚刚'
  }
  // 1小时内
  if (diffHours < 1) {
    return `${diffMinutes}分钟前`
  }
  // 24小时内
  if (diffDays < 1) {
    return `${diffHours}小时前`
  }
  // 昨天
  if (diffDays === 1) {
    return `昨天 ${date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })}`
  }
  // 前天
  if (diffDays === 2) {
    return `前天 ${date.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' })}`
  }
  // 7天内
  if (diffDays < 7) {
    return `${diffDays}天前`
  }
  
  // 超过7天，本年内
  if (isThisYear) {
    return date.toLocaleDateString('zh-CN', {
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit'
    })
  }
  
  // 不是本年
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

export function formatDetailTime(timestamp) {
  if (!timestamp) return ''
  const date = new Date(timestamp)
  const now = new Date()
  const isThisYear = now.getFullYear() === date.getFullYear()
  
  if (isThisYear) {
    return date.toLocaleString('zh-CN', {
      month: '2-digit',
      day: '2-digit',
      weekday: 'long',
      hour: '2-digit',
      minute: '2-digit',
      hour12: false
    })
  }
  
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    weekday: 'long',
    hour: '2-digit',
    minute: '2-digit',
    hour12: false
  })
} 